<script setup lang="ts" name="PageWrap">
import { propTypes } from '@/utils/propTypes'
import { storeToRefs } from 'pinia'
import { useAppStoreWithOut } from '@/store/modules/app'
import { useKeepAliveStoreWithOut } from '@/store/modules/keepAlive'
import { useTagCacheStoreWithOut } from '@/store/modules/tag'
import { clearPending } from '@/utils/axios/cancelToken'
const route = useRoute()
const { t } = useI18n() // 国际化

const appStore = useAppStoreWithOut()
const { systemConfig, pageLoading } = storeToRefs(appStore)

const keepAliveStore = useKeepAliveStoreWithOut()
const { scrollPosition, scrollTopHeight } = storeToRefs(keepAliveStore)

const tagCacheStore = useTagCacheStoreWithOut()
const { tagList } = storeToRefs(tagCacheStore)

const scrollbarRef: any = ref(null)

const { getPrefixCls, variables } = useDesign()
const prefixCls = getPrefixCls('page-wrap')
const scrollWrap = getPrefixCls('scroll-wrap')

const props = defineProps({
  title: propTypes.string.def(''),
  message: propTypes.string.def(''),
  bodyStyle: propTypes.object.def({ padding: '20px' }),
  scroll: propTypes.bool.def(false),
  direction: propTypes.string.def('column')
})

// 滚动监听执行事件
const handleScroll = (e) => {
  scrollTopHeight.value = e.scrollTop
}

// 路由跳转前执行事件
onBeforeRouteLeave(() => {
  const hasTag = tagList.value.some((item: any) => {
    return item.path === route.fullPath
  })
  if (hasTag && systemConfig.value && systemConfig.value.isKeepAlive && route.meta.keepAlive) {
    keepAliveStore.setScrollPosition({
      name: route.name,
      fullPath: route.fullPath,
      savedPosition: scrollTopHeight.value
    })
  }
  clearPending()
})

// 在组件挂载到页面后执行
onMounted(() => {
  scrollTopHeight.value = 0
})

// keep-alive启用时，页面被激活时使用
onActivated(() => {
  if (props.scroll && systemConfig.value.isKeepAlive && route.meta.keepAlive) {
    let hasRoute = false
    if (scrollPosition.value.length > 0) {
      scrollPosition.value.forEach((item: any) => {
        if (item.fullPath === route.fullPath) {
          hasRoute = true
          scrollbarRef.value.scrollTo(0, item.savedPosition)
        }
      })
      if (!hasRoute) {
        scrollbarRef.value.scrollTo(0, 0)
      }
    }
  }
})
</script>

<template>
  <ElScrollbar v-if="scroll" ref="scrollbarRef" v-loading="pageLoading" :wrap-class="scrollWrap" :view-class="[variables.namespace + '-scroll-view']" :class="prefixCls" @scroll="handleScroll">
    <slot />
    <Footer v-if="systemConfig.isFooterEnable" />
    <ElBacktop :title="t('common', 'backTop')" :target="'.' + scrollWrap" />
  </ElScrollbar>
  <div v-else :class="[prefixCls, 'is-no-scroll']">
    <div :class="`${prefixCls}-card`" :style="bodyStyle">
      <ElCard :body-style="bodyStyle" shadow="never">
        <template v-if="title" #header>
          <div class="flex items-center">
            <span class="text-16px font-700">{{ title }}</span>
            <ElTooltip v-if="message" effect="dark" placement="right">
              <template #content>
                <div class="max-w-200px">{{ message }}</div>
              </template>
              <SvgIcon :size="14" class="ml-5px" icon="ep:question-filled" />
            </ElTooltip>
            <div class="flex flex-grow pl-20px">
              <slot name="header" />
            </div>
          </div>
        </template>
        <slot />
      </ElCard>
    </div>
    <Footer v-if="systemConfig.isFooterEnable" />
  </div>
</template>

<style scoped lang="scss">
$prefix-cls: #{$namespace}-page-wrap;

.#{$prefix-cls} {
  display: flex;
  flex-direction: column;

  &-card {
    display: flex;
    flex: 1;
  }
}

.el-card {
  display: flex;
  background: transparent;
  border: none;
  border-radius: 0;
  flex: 1;
  flex-direction: column;

  :deep(.el-card__body) {
    display: flex;
    flex: 1;
    flex-direction: v-bind(direction);
    align-items: center;
    justify-content: center;
  }
}
</style>
